<?php
// 后台用户模块
class OrderAction extends CommonAction {
	protected $config=array('app_type'=>'master');
	
	function _search_filter(&$map) {
		if (!empty($_POST['keyword'])) {
			$map['emp_name|emp_no'] = array('like', "%" . $_POST['keyword'] . "%");
		}
	}

	/** 插入新新数据  **/
  protected function _insert() {
    //print_r('3333333333333333333333');
    //exit(333333);
    $name = $this -> getActionName();
    $model = D($name);
    if (false === $model -> create()) {
      $this -> error($model -> getError());
    }
    if (in_array('user_id', $model -> getDbFields())) {
      $model -> user_id = get_user_id();
      $model -> created_user_id = get_user_id();
    };
    if (in_array('user_name', $model -> getDbFields())) {
      $model -> user_name = $this -> _session("user_name");
    };
    //保存当前数据对象
    $list = $model -> add();
    if ($list !== false) {//保存成功

      $shenhe_title = '新建订单，需要审核';
      $shenhe_content = '新订单，请审核';
      $order_id = $list;

      $model = M('FlowType');
      $flow_type = $model -> find(12);
      $confirm = $flow_type['confirm'];
      $confirm_name = $flow_type['confirm_name'];

      $model = M('User');
      $flow_type = $model -> find(get_user_id());
      $user_name = $flow_type['emp_name'];
      $dept_id = $flow_type['dept_id'];
      $user_id = $flow_type['id'];

      $doc_no = '新建订单'; 

      $model = M('flow'); 
      $sql_1 = "INSERT INTO `gy_flow` (`add_file`,`type`,`confirm`,`confirm_name`,`consult`,`consult_name`,`refer`,`refer_name`,`step`,`title`,`content`,`is_del`,`create_time`,`user_id`,`user_name`,`dept_id`,`dept_name`,`doc_no`,`order_id`) VALUES ('','12','$confirm','$confirm_name','','','','',20,'$shenhe_title','$shenhe_content',0,1381135599,$user_id,'$user_name','$dept_id','南阳广溢信息技术有限公司','$doc_no','$order_id')";
      //$sql_2 = "INSERT INTO `gy_flow_log` (`flow_id`,`step`,`emp_no`,`create_time`) VALUES (88,21,'zhaoyaxin',$timenow)";
      $model -> execute($sql_1);
      $flow_id = $model -> order('id desc') -> limit(1) -> getField('id');
      //$model -> query($sql_2);
      //print_r($flow_id);
      //echo "888888888<br/>";

      $flow = new FlowModel();
      //function _conv_confirm($key,$val){
      $confirm_str = $flow -> _conv_confirm($flow_id,$confirm);
      $arr_confirm=explode(',',array_filter(explode("|",$confirm_str))[0]);
      $model = M('gy_flow_log');
      $timenow = time();
      foreach($arr_confirm as $emp_no){
        //print_r($emp_no);
        $sql_2 = "INSERT INTO `gy_flow_log` (`flow_id`,`step`,`user_id`,`user_name`,`emp_no`,`create_time`) VALUES ('$flow_id',21,'$user_id','$user_name','$emp_no','$timenow')";
        //print_r($sql_2);
        $re = $model -> execute($sql_2);
        //print_r($re);
      }
      //print_r($arr_confirm);
      //exit('kk');

      $this -> assign('jumpUrl', get_return_url());
      $this -> success('新增成功!');
    } else {
      //失败提示
      $this -> error('新增失败!');
    }
  }


	// 插入数据
	public function insert() {
	}

	function update() {
		$name = $this -> getActionName();
		$model = D($name);
		if (false === $model -> create()) {
			$this -> error($model -> getError());
		}
		// 更新数据
		$model -> __set('letter', get_letter($model -> __get('emp_name')));
		$list = $model -> save();
		if (false !== $list) {
			//成功提示
			$this -> assign('jumpUrl', get_return_url());
			$this -> success('编辑成功!');
		} else {
			//错误提示
			$this -> error('编辑失败!');
		}
	}

  function del($id=0) {
		$name = $this -> getActionName();
		$model = D($name);
    //print_r($name);
    //print_r($model);
    $model->delete($id);
    $this -> success('删除成功!');
    //print_r('成功删除一条信息!');
    //print_r($id);
  }


	function json() {
		header("Content-Type:text/html; charset=utf-8");
		$key = $_REQUEST['key'];

		$model = M("Order");
		$where['emp_name'] = array('like', "%" . $key . "%");
		$where['emp_no'] = array('like', "%" . $key . "%");
		$where['_logic'] = 'or';
		$map['_complex'] = $where;
		$list = $model -> where($map) -> field('id,emp_name as name') -> select();
		exit(json_encode($list));
	}

}
?>
